﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.CApAPPInitErrorDisplayer
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticPathPlanner;

public class CApAPPInitErrorDisplayer
{
  private List<string> m_nonProjectedLocations = new List<string>();
  private List<string> m_olpCommandErrorsLocations = new List<string>();
  private List<string> m_nonReachableLocations = new List<string>();
  private List<string> m_robotsWithNonValidJointProfile = new List<string>();
  private List<string> m_robotsWithNonValidRobotConfig = new List<string>();
  private bool m_areWaypointsCreatedWithJump;
  private CApAPPRoboticTask m_roboticTask;
  private List<string> m_simulationErrorMessages = new List<string>();
  private bool m_arelogicOLPCommandsDetected;
  private bool m_wasIgnoreLogicOLPCommands;
  private List<List<string>> m_unknownZoneNames = new List<List<string>>();
  private bool m_isRobotDefaultController;
  private bool m_hasNoDevice;
  private bool m_areTasksInvalid;

  public bool IsUnknowZoneNamesError => this.m_unknownZoneNames.Count > 0;

  public bool AreTasksInvalid => this.m_areTasksInvalid;

  public CApAPPInitErrorDisplayer(CApAPPTaskManager taskManager, bool wasIgnoreLogicOLPCommands)
  {
    this.m_wasIgnoreLogicOLPCommands = wasIgnoreLogicOLPCommands;
    this.m_areTasksInvalid = false;
    foreach (CApAPPTask task in taskManager.Tasks)
    {
      if (!task.IsValid)
      {
        this.m_areTasksInvalid = true;
        break;
      }
    }
    if (this.m_areTasksInvalid)
      return;
    for (int index = 0; index < taskManager.CountTasks(); ++index)
    {
      if (taskManager.Tasks[index] is CApAPPRoboticTask task2)
      {
        this.m_nonProjectedLocations.AddRange((IEnumerable<string>) task2.NonProjectedLocationNames);
        this.m_olpCommandErrorsLocations.AddRange((IEnumerable<string>) task2.GetNotLockedAndNotFixedOLPCommandsLocationNames());
        this.m_nonReachableLocations.AddRange((IEnumerable<string>) task2.NonReachableCommandsNames);
        if (!task2.IsJointProfileValidForAPP && !CApAPPUtilities.IsRobotConnectedToRCS(task2.Robot))
          this.m_robotsWithNonValidJointProfile.Add(((ITxObject) task2.Robot).Name);
        if (!task2.IsRobotConfigValidForApp)
          this.m_robotsWithNonValidRobotConfig.Add(((ITxObject) task2.Robot).Name);
        if (task2.AreWaypointsCreatedWithJump)
        {
          this.m_areWaypointsCreatedWithJump = true;
          this.m_roboticTask = task2;
          this.m_simulationErrorMessages = task2.LastSimulationErrorMessages;
        }
        if (!this.m_arelogicOLPCommandsDetected)
          this.m_arelogicOLPCommandsDetected = task2.doesContainsLogicOLPCommands();
        if (task2.Robot == null)
          this.m_hasNoDevice = true;
        this.CheckZoneNames(task2);
      }
      else if (taskManager.Tasks[index] is CApAPPGunOnlyObjectFlowTask task1)
      {
        ITxWeldOperation weldOperation = task1.WeldOperation;
        if (weldOperation != null && weldOperation.Gun == null)
          this.m_hasNoDevice = true;
      }
    }
  }

  public void Display(ref bool shouldContinue)
  {
    if (this.ShouldDisplay())
      shouldContinue = this.ShowErrorReport(this.ShouldAppBeAllowedToContinue());
    else
      shouldContinue = true;
  }

  private bool ShouldDisplay()
  {
    return this.m_nonProjectedLocations.Count > 0 || this.m_olpCommandErrorsLocations.Count > 0 || this.m_nonReachableLocations.Count > 0 || this.m_robotsWithNonValidJointProfile.Count > 0 || this.m_robotsWithNonValidRobotConfig.Count > 0 || this.m_areWaypointsCreatedWithJump || this.m_unknownZoneNames.Count > 0 || this.m_hasNoDevice || this.m_areTasksInvalid;
  }

  private bool ShouldAppBeAllowedToContinue() => !this.m_areTasksInvalid;

  private bool ShowErrorReport(bool isAllowedToContinue)
  {
    bool? nullable = new CUiAPPErrorWindow(CApAPPMainWindowStringTable.ProblemDetectedWithOperaitonMsg, this.BuildMessage().ToString(), isAllowedToContinue ? CUiAPPErrorWindow.ButtonState.Both : CUiAPPErrorWindow.ButtonState.AbortOnly).ShowDialog(true);
    return nullable.HasValue && nullable.Value;
  }

  private StringBuilder BuildMessage()
  {
    StringBuilder messageBuilder = new StringBuilder();
    if (this.m_nonReachableLocations.Count > 0)
    {
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.NonReachableLocationsMsg);
      foreach (string reachableLocation in this.m_nonReachableLocations)
        messageBuilder.AppendLine(reachableLocation);
    }
    if (this.m_nonProjectedLocations.Count > 0)
    {
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.NonProjectedLocationsMsg);
      foreach (string projectedLocation in this.m_nonProjectedLocations)
        messageBuilder.AppendLine(projectedLocation);
    }
    if (this.m_olpCommandErrorsLocations.Count > 0)
    {
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.LocationsWithUnsupportedOLPCommandsMsg);
      foreach (string commandErrorsLocation in this.m_olpCommandErrorsLocations)
        messageBuilder.AppendLine(commandErrorsLocation);
      messageBuilder.AppendLine(CApAPPMainWindowStringTable.DeleteLocationsWithOLPCommandsMsg);
      messageBuilder.AppendLine(CApAPPMainWindowStringTable.OlpCommandsBackupOpWarnMsg);
    }
    if (this.m_robotsWithNonValidJointProfile.Count > 0)
    {
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.InvalidJointProfileCollidingMotions);
      foreach (string str in this.m_robotsWithNonValidJointProfile)
        messageBuilder.AppendLine(str);
      messageBuilder.AppendLine(CApAPPMainWindowStringTable.InvalidJointProfileEditMotionParameters);
    }
    if (this.m_robotsWithNonValidRobotConfig.Count > 0)
    {
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.InvalidRobotConfigUnexpectedResults);
      foreach (string str in this.m_robotsWithNonValidRobotConfig)
        messageBuilder.AppendLine(str);
      messageBuilder.AppendLine(CApAPPMainWindowStringTable.InvalidRobotConfigEditMotionParameters);
    }
    if (this.m_areWaypointsCreatedWithJump)
    {
      string sectionMessage = string.Format(CApAPPMainWindowStringTable.WarningWaypointsCreatedWithJump, this.m_roboticTask != null ? (object) this.m_roboticTask.Name : (object) "", (object) this.m_simulationErrorMessages.FirstOrDefault<string>());
      this.AppendNewSectionToMessageBuilder(messageBuilder, sectionMessage);
    }
    if (this.m_arelogicOLPCommandsDetected && !this.m_wasIgnoreLogicOLPCommands)
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.WarningLogicOLPDeactivated);
    if (this.m_unknownZoneNames.Count > 0)
    {
      if (this.m_isRobotDefaultController)
        this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.WarningUnknownZonesDefaultController);
      else
        this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.WarningUnknownZones);
      foreach (List<string> unknownZoneName in this.m_unknownZoneNames)
      {
        StringBuilder stringBuilder = new StringBuilder();
        foreach (string str in unknownZoneName)
        {
          stringBuilder.Append(str);
          stringBuilder.Append(" ");
        }
        messageBuilder.AppendLine(stringBuilder.ToString());
      }
    }
    if (this.m_hasNoDevice)
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.WarningNoDevice);
    if (this.m_areTasksInvalid)
      this.AppendNewSectionToMessageBuilder(messageBuilder, CApAPPMainWindowStringTable.InvalidTasksError);
    return messageBuilder;
  }

  private void AppendNewSectionToMessageBuilder(StringBuilder messageBuilder, string sectionMessage)
  {
    if (messageBuilder.Length > 0)
    {
      messageBuilder.AppendLine();
      messageBuilder.AppendLine("________________________________________");
      messageBuilder.AppendLine();
    }
    messageBuilder.AppendLine(sectionMessage);
  }

  private void CheckZoneNames(CApAPPRoboticTask roboticTask)
  {
    this.m_isRobotDefaultController = roboticTask.Robot.Controller.Name == "default";
    string[][] motionZoneNames = roboticTask.GetMotionZoneNames((TxMotionType) 2);
    roboticTask.GetMotionZoneNames((TxMotionType) 1);
    List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(roboticTask.Robot, roboticTask.Operation);
    List<List<string>> stringListList1 = new List<List<string>>();
    foreach (string zoneString in zoneParameters)
    {
      List<string> zoneParameterValues = CApAPPUtilities.GetAllowableZoneParameterValues(roboticTask.Robot, roboticTask.Operation, zoneString, (TxMotionType) 2);
      stringListList1.Add(zoneParameterValues);
    }
    List<List<string>> stringListList2 = new List<List<string>>();
    foreach (string zoneString in zoneParameters)
    {
      List<string> zoneParameterValues = CApAPPUtilities.GetAllowableZoneParameterValues(roboticTask.Robot, roboticTask.Operation, zoneString, (TxMotionType) 1);
      stringListList2.Add(zoneParameterValues);
    }
    for (int index1 = 0; index1 < motionZoneNames.Length; ++index1)
    {
      string[] source = motionZoneNames[index1];
      bool flag = false;
      if (source.Length == stringListList1.Count)
      {
        for (int index2 = 0; !flag && index2 < source.Length; ++index2)
        {
          string str = source[index2];
          List<string> stringList = stringListList1[index2];
          for (int index3 = 0; !flag && index3 < stringList.Count; ++index3)
          {
            if (stringList[index3] == str)
              flag = true;
          }
        }
      }
      if (!flag)
        this.m_unknownZoneNames.Add(((IEnumerable<string>) source).ToList<string>());
    }
  }
}
